<?php

//button
function get_default_button($meta_id, $post_id, $post_type, $value_array, $user_create, $disable, $obj = null)
{
    $html_button = '';
    $class = 'btn_answer';
    if ($disable == true) {
        $class = '';
    }
    if ($post_type == 4) {
        $anwser_list = $obj->getAveragePostAnswerSlider($post_id);
        $html_button .= '<link rel="stylesheet" href="http://' . $_SERVER['HTTP_HOST'] . '/public/js/jrating/jRating.jquery.css" type="text/css" />';
        $html_button .= '<script type="text/javascript" src="http://' . $_SERVER['HTTP_HOST'] . '/public/js/jrating/jRating.jquery.js"></script>';
        $html_button .= '<div class="jrating" user_create="'.$user_create.'" data-average="' . $anwser_list[0]['avg'] . '" data-id="1" data_post_id="' . $post_id . '"  data_meta_id="' . $meta_id . '" data_user_id="' . $_SESSION["authenticated"] . '" data_type="' . $post_type . '" ></div>';
        $html_button .= '<!-- JS to add -->
                        <script type="text/javascript">
                          $(document).ready(function(){
                            $(".jrating").jRating({
                            length : 5,
			            	decimalLength : 1,
                              onSuccess : function(data){
                                //alert(data);
                              },
                              onError : function(){
                                alert(\'Error : please retry\');
                              }
                            });
                          });
                        </script>';
        $html_button .= '';

    }
    else {
        $count = count($value_array);
        $i = 0;
        $class_separate = '';
        foreach ($value_array as $value) {
            $i++;
            $width = 100 / $count;
            if($i >1 && $i < $count){
                $class_separate = 'separate_answer';
            }

            $html_button .= '<div class="grid_3">
                <button  class="' . $class . ' btn_blue '.$class_separate.'" user_create="' . $user_create . '"  data_post_id="' . $post_id . '"  data_meta_id="' . $meta_id . '" data_user_id="' . $_SESSION["authenticated"] . '" style="width:' . $width . '%" data_type="' . $post_type . '" data_value="' . $value . '" >';
            $html_button .= $value;
            $html_button .= '</button> </div>';

        }
    }
    return $html_button;
}

function get_slider_html($meta_id, $post_id, $post_type, $value_array, $user_create, $disable,$extra_value='')
{

    $value = 1;
    $html_button = '';
    $class = 'btn_answer';
    if ($disable == true) {
        $class = '';
    }


    $avg = ($value_array[0] + $value_array[1])/2;
    $html_button .= '<input type="text" class="minimum-val"  value="'.$value_array[0].'" >';
    $html_button .= '<input type="text" class="maximun-val"  value="'.$value_array[1].'" >';
    $html_button .= '<input type="text" class="amount-slider" id="amount-'.$post_id.'" value="'.$avg.'" >';
    $html_button .= '<span class="slider-extra-value">'.$extra_value.'</span>';
    $html_button .= '<div id="slider-range-max-'.$post_id.'" style="margin: 5px 0px"></div>';
    $html_button .= '<button id="btn_vote_' . $meta_id . '" user_create="' . $user_create . '"  class="btn-slider-vote ' . $class . '"data_value="'.$avg.'" style="" data_post_id="' . $post_id . '"  data_meta_id="' . $meta_id . '" data_user_id="' . $_SESSION["authenticated"] . '" data_type="' . $post_type . '" data_value="' . $value . '" >Vote</button>';
    $html_button .= '<script>
              $(function() {
              var data = $(".btn_answer").attr("data_avg");

                $( "#slider-range-max-'.$post_id.'" ).slider({
                  range: "max",
                  min: ' . $value_array[0] . ',
                  max: ' . $value_array[1] . ',
                  value: "'.$avg.'",
                  slide: function( event, ui ) {
                    $( "#amount-'.$post_id.'" ).val( ui.value );
                    $("#btn_vote_' . $meta_id . '").attr("data_value",ui.value);
                    var div = "<div class=\'round-hand\'></div>";
                    var count = $(this).children(\'.ui-slider-range\').find(".round-hand").length;
                     if(count < 1){
                        $(this).children(\'.ui-slider-range\').append(div);
                     }

                     $(this).children(\'.ui-slider-handle\').remove();
                  }
                });
                $( "#amount-'.$post_id.'" ).val( $( "#slider-range-max-'.$post_id.'" ).slider( "value" ) );
              });
              </script>';

//    $html_button .= '</div>';

    return $html_button;
}

function get_star_html($meta_id, $post_id, $post_type, $value_array, $user_create, $disable)
{
    $class = 'btn_answer';
    if ($disable == true) {
        $class = '';
    }
    $html_button = '';
    foreach ($value_array as $value) {
        $html_button .= '<div class="grid_3">
                <button class="' . $class . '"  user_create="' . $user_create . '" data_value="' . $value . '" style="float: left; margin-bottom: 10px; width: 100%; padding: 10px; cursor: pointer" data_post_id="' . $post_id . '"  data_meta_id="' . $meta_id . '" data_user_id="' . $_SESSION["authenticated"] . '" data_type="' . $post_type . '" >';
        $html_button .= $value;
        $html_button .= '</button> ';
    }
    return $html_button;
}

//end button

//html answer
function get_default_answer($obj_data, $post_id)
{

    $html = '';
    $answer_list = $obj_data->gettanswerCount($post_id);

    $info_list = array();
    $i=0;

    foreach ($answer_list['list'] as $answer) {

        $col = 100;
        if (count($answer_list['list']) != 0) {
            //$col = 100/ count($answer_list['list'] );
        }
        $percent = round(($answer['count'] / $answer_list['total'][0]['count_answer']) * 100, 2);
        $info_list[$i++] = array('percent'=>$percent,
                                 'value'=> $answer['count'],
                                 'title'=> $answer['post_answer_value']);
    }
    return $info_list;
}

function get_slider_answer($obj_data, $post_id, $title = 'AVG', $subfix = '',$post_question)
{
    $anwser_list = $obj_data->getAveragePostAnswerSlider($post_id);
    $obj = new AjaxRequest();


    $html_slider = $obj->sliderSolid($post_id,'sliderChart',number_format($anwser_list[0]['avg'], 1),$title,$subfix, $post_question[0], $post_question[1]);

    $html_text = '<div style="float: left;
width: 100%;
font-weight: bold;
font-size: 25px;">' . number_format($anwser_list[0]['avg'], 1) .' '.$subfix. ' </div>';
    return $html_slider.$html_text;
//    return $html;
}
function get_star_answer($obj_data, $post_id, $title = 'AVG', $subfix = '',$post_question)
{
    $anwser_list = $obj_data->getAveragePostAnswerSlider($post_id);
    $html = '<div style="float: left; width:100%">' . $title . ' : ' . number_format($anwser_list[0]['avg'], 1) .' '.$subfix. ' </div>';
    return $html;
//    return $html;
}

//end html answer


//chart
function binarychart($array_info,$idPost=null){
    $dataText = '';
    for($i=0; $i<= count($array_info) - 2; $i++){
        $dataText .= '
            ["'.$array_info[$i]['title'].'",   '.$array_info[$i]['percent'].'],
        ';
    }
    $dataText .= '
            ["'.$array_info[$i]['title'].'",   '.$array_info[$i]['percent'].']
        ';

    $obj = new AjaxRequest();
    $html = $obj->renderPieChart($dataText,$idPost,'postChart',array('dataLabels'=>'true'));
    return $html;
}

function multichart($array_info, $post_question){
    $html = '<ul class="multi-chart">';
    for($i = 0 ; $i < count($post_question); $i++){
        $chart = $array_info[$i];
        $percent = $chart['percent']?$chart['percent']:0;
        $val = $chart['value']?$chart['value']:0;
        $top = 100 - $percent -15;
        $width = 100/count($post_question);
        $html .= '<li style=" width: '.$width.'%">
            <div class="barchart" style="height:'.$percent.'%; top: '.$top.'% ">
            </div>
            <p>'.$post_question[$i].'-'.$val.' votes ('. $percent .'% ) </p>
        </li>';
    }
    return $html;
}
//end chart
?>